/*
 * send ajax requests
 * @param type      [POST,GET]
 * @param url       url of requested page
 * @param data      the data which send to database
 * @param success   function which be callad after request successed
 *                  also content the response in first paramter
 * @error error     function called on failer    
 */
MAX = {}
MAX.ajax = function(type,url,data,success,error){
    $.ajax({
        type:type,
        url:url,
        data:data,
        success:success,
        error:error
    })
}

/*
 * block site page
 * @param title : title of waiting page
 */
MAX.blockPage = function (title){
    var theme = true
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        theme=false
    }
    $.blockUI({
        theme:     theme, 
        message:  '<p><center>'+__('Please wait')+'.....<div class="wait">&nbsp;</div></center></p>',
        title: title,
        css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            color: '#fff' 
        }
    });
}

/*
 * unblock div untill ajax finished
 */
MAX.unBlockPage = function (){
    $.unblockUI();
}

///*
// *
// */
//MAX.createDialog = function(element)
//{
//    $(element).dialog({
//        width: 'auto',
//        draggable: false,
//        resizable: false,
//        modal: true,
//        autoOpen: false
//    });
//}
///*
// * show dialog box
// * @param element - the element which we want to show in dialog
// */
//MAX.openDialog = function(element)
//{
//    $(element).dialog('open');
//}
///*
// * close dialog box
// * @param element - the element which we want to close dialog
// */
//MAX.closeDialog = function(element)
//{
//    $(element).dialog('close')
//}
/*
 * show confirm message
 * @param msg - the message which will be show to user
 * @return boolean  
 */
MAX.confirmDialog = function(msg,conftirmFunction)
{
    var value=''
    $('body').append('<div id="confirm_msg">'+msg+'</div>')
    $('#confirm_msg').dialog({
        width: 'auto',
        draggable: false,
        resizable: false,
        modal: true,
        buttons: {
            'ok': function() {
                conftirmFunction()
                $( this ).dialog( "close" );
            },
            "cancel": function() {
                $( this ).dialog( "close" );
            }
        }
    });
}
	

/*
 * @class HashPager used for paging using ajax
 * @config must include jquery.unserializeForm.js plugin
 * @param baseDivElement - the element which used for delegate -
 * @param listDivElement - the element which will content the list (grid) html -
 * @param urlOfFirstPage - the url which used to get the first page (first reqeust)-
 * @param form element   - the form which using to filter data
 * @param callBackFunctionAfterRequestSuccess -option function run after list added to listDivElement -
 * <p>to use this class you need to send this paramters and the call initPager() function</p>
 * <b>Examples:</b>
 * <code>
 *  <br />var pager = new HashPager($('#userList'), $('#userList'),$('#userListURL').val()) ;
 *  <br />pager.initPager() ;
 * </code>
 * @auther Mina Gerges
 */
function HashPager(baseDivElement,listDivElement,urlOfFirstPage,formElement,callBackFunctionAfterRequestSuccess){
    this.baseDivElement = baseDivElement
    this.listDivElement = listDivElement;
    this.urlOfFirstPage = urlOfFirstPage;
    this.formElement = formElement;
    this.callBackFunctionAfterRequestSuccess = callBackFunctionAfterRequestSuccess
    this.formElementFlag = false
    // pager configuration
    var toThis = this
    // to handle hash change
    $(window).hashchange( function(){
        toThis.loadPage()
    })
    // start the pager
    this.initPager = function()
    {
        var toThis = this ;
        $(toThis.baseDivElement).delegate('#thePagerDiv a','click',function(){
            var href = $(this).attr('href')
            window.location.hash = href
            return false
        })
        if(this.formElement)
        {
            this.formElementFlag = true
            $(this.formElement).find('input, select').change(function(){
                window.location.hash = toThis.urlOfFirstPage+'?'+$(toThis.formElement).serialize();
            })
        }
        this.loadPage()
        return true 
    }
    
    this.loadFirstPage = function()
    {
        var hash = window.location.hash
        hash = hash.replace('#','');
        if(hash == this.urlOfFirstPage)
            this.loadPage()
        else
            window.location.hash = this.urlOfFirstPage
    }
    // load tha ajax requests
    this.loadPage = function()
    {
        var hash = window.location.hash
        hash = hash.replace('#','');
            
        var url = ''
        if(hash)
        {
            url = hash
            if(this.formElementFlag)
            {
                var formStr = hash.split('?')
                $(this.formElement).unserializeForm(formStr[1],{
                    'override-values' : true
                })
            }
        }
        else
        {
            url = toThis.urlOfFirstPage
        }
        //        $(toThis.listDivElement).html('');
        var data = ''
        if(toThis.formElementFlag)
        {
            data = $(toThis.formElement).serialize();
            
        }
        MAX.ajax('POST', url, data, function(res)
        {
            var html = ''
            if(typeof res =='object')
            {
                html = res.html
            }
            else
            {
                html = res
            }
            $(toThis.listDivElement).html(html);
            // run callBackFunctionAfterRequestSuccess if its sended
            if(toThis.callBackFunctionAfterRequestSuccess && typeof toThis.callBackFunctionAfterRequestSuccess == 'function')
            {
                toThis.callBackFunctionAfterRequestSuccess(res)
            }
        })
    }   

}

function DialogBox(element)
{
    this.element  = element
    var that = this
    var createDialog = function()
    {
        $(that.element).dialog({
            width: 'auto',
            draggable: false,
            resizable: false,
            modal: true,
            autoOpen: false
        });
    }
    createDialog()
    
    this.open = function()
    {
         $(that.element).dialog('open')
    }
    this.close = function()
    {
         $(that.element).dialog('close')
    }
    this.html = function(html)
    {
        $(that.element).html(html)
    }
}

/**
 * Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
 * @param obj1
 * @param obj2
 * @returns obj3 a new object based on obj1 and obj2
 */
function merge_options(obj1,obj2){
    var obj3 = {};
    for (var attrname in obj1) {
        obj3[attrname] = obj1[attrname];
    }
    for (var attrname in obj2) {
        obj3[attrname] = obj2[attrname];
    }
    return obj3;
}